﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using UFC.Common;
using System.Configuration;
namespace SendToWCAPPDispatchInfo
{
    public partial class Send_DispatchInfo : ServiceBase
    {
        private System.Timers.Timer m_SyncTimer, m_SyncTimerTDM;
        private System.Timers.Timer m_SyncTimerWCAPP;
        public Send_DispatchInfo()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            LogHelp.SetConfig();
            int intSync = Convert.ToInt32(ConfigurationManager.AppSettings["sync"].ToString());
            m_SyncTimer = new System.Timers.Timer();
            m_SyncTimer.Enabled = false;
            m_SyncTimer.Elapsed += new System.Timers.ElapsedEventHandler(m_SyncTimer_Elapsed);

            m_SyncTimer.Interval = 1000*60;
            m_SyncTimer.Enabled = true;
            m_SyncTimer.Start();

            //WCAPP定时器
            m_SyncTimerWCAPP = new System.Timers.Timer();
            m_SyncTimerWCAPP.Enabled = false;
            m_SyncTimerWCAPP.Elapsed += new System.Timers.ElapsedEventHandler(m_SyncTimer_ElapsedWCAPP);

            m_SyncTimerWCAPP.Interval = intSync *10*60* 1000;//*1440
            m_SyncTimerWCAPP.Enabled = true;
            m_SyncTimerWCAPP.Start();

            //TDM定时器 add YangJiang 20180702
            m_SyncTimerTDM = new System.Timers.Timer();
            m_SyncTimerTDM.Enabled = false;
            m_SyncTimerTDM.Elapsed += new System.Timers.ElapsedEventHandler(m_SyncTimer_ElapsedTDM);

            m_SyncTimerTDM.Interval = intSync*10*60*1000;
            m_SyncTimerTDM.Enabled = true;
            m_SyncTimerTDM.Start();

            
        }
        
        private void m_SyncTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
               // LogHelp.WriteLog("派工信息结转启动");
                m_SyncTimer.Enabled = false;
                SendDispatchInfo runMain = new SendDispatchInfo();
                runMain.RunDispatchTableSync();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError(myError.Message, myError);
            }
            finally
            {
                m_SyncTimer.Enabled = true;
            }
        }
        //WCAPP
        private void m_SyncTimer_ElapsedWCAPP(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                //LogHelp.WriteLog("派工推送服务启动");
                m_SyncTimerWCAPP.Enabled = false;
                SendDispatchInfo runMain = new SendDispatchInfo();
                runMain.RunSendDispatchSyncToWCAPP();
            }
            catch (Exception myError)
            {
                LogHelp.WriteError(myError.Message, myError);
            }
            finally
            {
                m_SyncTimerWCAPP.Enabled = true;
            }
        }

        //TDM add YangJiang 20180702
        private void m_SyncTimer_ElapsedTDM(object sender, System.Timers.ElapsedEventArgs e)
        {
            try
            {
                //LogHelp.WriteLog("试车任务推动服务启动");
                m_SyncTimerTDM.Enabled = false;
                SendDispatchInfo runMain = new SendDispatchInfo();
                runMain.RunSendDispatchSyncToTDMSCTask();// 试车派工数据结转
                runMain.RunSendDispatchSyncToTDMPartTask();// 部件试车派工数据结转
            }
            catch (Exception myError)
            {
                LogHelp.WriteError(myError.Message, myError);
            }
            finally
            {
                m_SyncTimerTDM.Enabled = true;
            }
        }

        protected override void OnStop()
        {
        }
    }
}
